IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[TBL_DOC_CAT_Select]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[TBL_DOC_CAT_Select]
GO
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[TBL_DOC_CAT_SelectAll]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[TBL_DOC_CAT_SelectAll]
GO
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[TBL_DOC_CAT_Save]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[TBL_DOC_CAT_Save]
GO
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[TBL_DOC_CAT_Delete]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[TBL_DOC_CAT_Delete]
GO
CREATE PROC [dbo].[TBL_DOC_CAT_Select] @DOC_CAT_ID int, @DOC_CAT_TYPE varchar(50), @DOC_CAT_DESC varchar(MAX), @DOC_CAT_ACTIVE bit
AS
SELECT * FROM [dbo].[TBL_DOC_CAT] WHERE [DOC_CAT_ID]=@DOC_CAT_ID AND [DOC_CAT_TYPE]=@DOC_CAT_TYPE AND [DOC_CAT_DESC]=@DOC_CAT_DESC AND [DOC_CAT_ACTIVE]=@DOC_CAT_ACTIVE
GO
CREATE PROC [dbo].[TBL_DOC_CAT_Delete] @DOC_CAT_ID int, @DOC_CAT_TYPE varchar(50), @DOC_CAT_DESC varchar(MAX), @DOC_CAT_ACTIVE bit
AS
DELETE FROM [dbo].[TBL_DOC_CAT] WHERE [DOC_CAT_ID]=@DOC_CAT_ID AND [DOC_CAT_TYPE]=@DOC_CAT_TYPE AND [DOC_CAT_DESC]=@DOC_CAT_DESC AND [DOC_CAT_ACTIVE]=@DOC_CAT_ACTIVE
GO
CREATE PROC [dbo].[TBL_DOC_CAT_SelectAll]
AS
SELECT * FROM [dbo].[TBL_DOC_CAT]
GO
CREATE PROC [dbo].[TBL_DOC_CAT_Save] @DOC_CAT_ID int, @DOC_CAT_TYPE varchar(50), @DOC_CAT_DESC varchar(MAX), @DOC_CAT_ACTIVE bit
AS
IF EXISTS (SELECT * FROM [dbo].[TBL_DOC_CAT] WHERE [DOC_CAT_ID]=@DOC_CAT_ID AND [DOC_CAT_TYPE]=@DOC_CAT_TYPE AND [DOC_CAT_DESC]=@DOC_CAT_DESC AND [DOC_CAT_ACTIVE]=@DOC_CAT_ACTIVE)
BEGIN
	--update existing record
	UPDATE [dbo].[TBL_DOC_CAT] SET [DOC_CAT_TYPE]=@DOC_CAT_TYPE, [DOC_CAT_DESC]=@DOC_CAT_DESC, [DOC_CAT_ACTIVE]=@DOC_CAT_ACTIVE
	WHERE [DOC_CAT_ID]=@DOC_CAT_ID AND [DOC_CAT_TYPE]=@DOC_CAT_TYPE AND [DOC_CAT_DESC]=@DOC_CAT_DESC AND [DOC_CAT_ACTIVE]=@DOC_CAT_ACTIVE
END
ELSE
BEGIN
	--insert new record
	INSERT INTO [dbo].[TBL_DOC_CAT] ([DOC_CAT_TYPE], [DOC_CAT_DESC], [DOC_CAT_ACTIVE])
	VALUES (@DOC_CAT_TYPE, @DOC_CAT_DESC, @DOC_CAT_ACTIVE)
	SET @DOC_CAT_ID=SCOPE_IDENTITY()
END

SELECT * FROM [dbo].[TBL_DOC_CAT] WHERE [DOC_CAT_ID]=@DOC_CAT_ID AND [DOC_CAT_TYPE]=@DOC_CAT_TYPE AND [DOC_CAT_DESC]=@DOC_CAT_DESC AND [DOC_CAT_ACTIVE]=@DOC_CAT_ACTIVE
GO
